//
//  MyStationViewController.m
//  TengyunStation
//
//  Created by Johnny on 15/8/26.
//  Copyright (c) 2015年 xingrong. All rights reserved.
//

#import "MyStationViewController.h"
#import "DaishouExpressViewController.h"
#import "ThisMonBillViewController.h"
#import "SettingViewController.h"
#import "UserMessageViewController.h"
#import "MessageViewController.h"
#import "MoneyViewController.h"
#import "MyStationCell.h"
#import "Singleton.h"
#import "TSHeader.h"
#import "RejectViewController.h"

@interface MyStationViewController ()

@end

@implementation MyStationViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.title = @"个人中心";
    }
    return self;
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self.tableView reloadData];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    _websiteCodeLabel.text = [Singleton shareInstance].userMessage.websiteCode;
    _websiteNameLabel.text = [Singleton shareInstance].userMessage.websiteName;
    _starView.scorePercent = [Singleton shareInstance].userMessage.websiteGrade/5.0;
    _headerView.frame = CGRectMake(0, 0, 320, 150);
    self.tableView.tableHeaderView = _headerView;
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(pullUserMessageVC)];
    tap.numberOfTapsRequired = 1;
    [_headerView addGestureRecognizer:tap];
    
    [self setSettingButton];
}

- (void)setSettingButton {
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(0, 0, 22, 22);
    [button setBackgroundColor:[UIColor clearColor]];
    [button setImage:[UIImage imageNamed:@"nav_icon_shez"] forState:UIControlStateNormal];
    [button addTarget:self action:@selector(settingButtonAction) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button];
}

- (void)settingButtonAction {
    SettingViewController *settingVC = [[SettingViewController alloc] initWithNibName:@"SettingViewController" bundle:nil];
    [self.navigationController pushViewController:settingVC animated:YES];
}

- (void)pullUserMessageVC {
    UserMessageViewController *usermessageVC = [[UserMessageViewController alloc] initWithNibName:@"UserMessageViewController" bundle:nil];
    [self.navigationController pushViewController:usermessageVC animated:YES];
}

#pragma mark UITableViewDataSource,UITableViewDelegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 4;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    if (section == 0) {
        return 12;
    }
    return 10;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 0;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIndentifier = @"MyStationCell";
    MyStationCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIndentifier];
    if (!cell) {
        cell = [[[NSBundle mainBundle] loadNibNamed:@"MyStationCell" owner:self options:nil] lastObject];
    }
    if (indexPath.section == 0) {
        cell.logoImageView.image = [UIImage imageNamed:@"myxz_icon_daijie"];
        cell.nameLabel.text = @"待收快件";
        if ([Singleton shareInstance].statisticsCount.waitExpressesCount > 0) {
            cell.detailLabel.textColor = OrangeColor;
        } else {
            cell.detailLabel.textColor = TipTextColor;
        }
        cell.detailLabel.text = [NSString stringWithFormat:@"%d批快件待接收",[Singleton shareInstance].statisticsCount.waitExpressesCount];
    }
//    else if (indexPath.section == 1) {
//        cell.logoImageView.image = [UIImage imageNamed:@"myxz_icon_zhanh"];
//        cell.nameLabel.text = @"账户余额";
//        float money = [Singleton shareInstance].statisticsCount.allWithdraw + [Singleton shareInstance].statisticsCount.frozenMoney;
//        cell.detailLabel.text = [NSString stringWithFormat:@"￥%.2f",money];
//        cell.detailLabel.textColor = OrangeColor;
//    }
    else if (indexPath.section == 1) {
        cell.logoImageView.image = [UIImage imageNamed:@"myxz_icon_xiaox"];
        cell.nameLabel.text = @"我的消息";
        if ([Singleton shareInstance].statisticsCount.noReadCount > 0) {
            cell.detailLabel.textColor = OrangeColor;
        } else {
            cell.detailLabel.textColor = TipTextColor;
        }
        cell.detailLabel.text = [NSString stringWithFormat:@"%d条消息未读",[Singleton shareInstance].statisticsCount.noReadCount];
    } else if (indexPath.section == 2) {
        cell.logoImageView.image = [UIImage imageNamed:@"myxz_icon_zhangd"];
        cell.nameLabel.text = @"月结账单";
        cell.detailLabel.text = @"查看月结快件";
        cell.detailLabel.textColor = TipTextColor;
    } else if (indexPath.section == 3) {
        cell.logoImageView.image = [UIImage imageNamed:@"myxz_icon_jusyh"];
        cell.nameLabel.text = @"拒收用户";
        cell.detailLabel.text = [NSString stringWithFormat:@"%d个拒收用户",[Singleton shareInstance].statisticsCount.rejectUserCount];
        cell.detailLabel.textColor = OrangeColor;
    }
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (indexPath.section == 0) {
        [MobClick event:@"gerenzhongxin_btn_daishoukuaidi"];
        DaishouExpressViewController *daishouEVC = [[DaishouExpressViewController alloc] initWithNibName:@"DaishouExpressViewController" bundle:nil];
        [self.navigationController pushViewController:daishouEVC animated:YES];
    }
//    else if (indexPath.section == 1) {
//        MoneyViewController *moneyVC = [[MoneyViewController alloc] initWithNibName:@"MoneyViewController" bundle:nil];
//        [self.navigationController pushViewController:moneyVC animated:YES];
//    }
    else if (indexPath.section == 1) {
        [MobClick event:@"gerenzhongxin_btn_xiaoxi"];
        MessageViewController *messageVC = [[MessageViewController alloc] initWithNibName:@"MessageViewController" bundle:nil];
        [self.navigationController pushViewController:messageVC animated:YES];
    } else if (indexPath.section == 2) {
        [MobClick event:@"gerenzhongxin_btn_yuejiezhangdan"];
        ThisMonBillViewController *thismonBVC = [[ThisMonBillViewController alloc] initWithNibName:@"ThisMonBillViewController" bundle:nil];
        [self.navigationController pushViewController:thismonBVC animated:YES];
    } else if (indexPath.section == 3) {
        RejectViewController *rejectVC = [[RejectViewController alloc] initWithNibName:@"RejectViewController" bundle:nil];
        [self.navigationController pushViewController:rejectVC animated:YES];
    }
}

@end
